package sg.com.techstudio.tafep.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import sg.com.techstudio.tafep.email.GenericHTMLPreparator;
import sg.com.techstudio.tafep.filter.ParticipantFilter;
import sg.com.techstudio.tafep.filter.RegisterFilter;
import sg.com.techstudio.tafep.obj.ParticipantObj;
import sg.com.techstudio.tafep.obj.RegisterObj;
import sg.com.techstudio.tafep.util.NumberToolSg;

@Component
public class EmailDao {
	private static final Log log = LogFactory.getLog(EmailDao.class);
	@Autowired
	private VelocityEngine velocityEngine;
	
	@Autowired
	private JavaMailSender mailSender;
	
	@Autowired
	private EmailConfig confirmationEmailConfig
		, participantEmailConfig, othercompanyEmailConfig
		, chequeEmailConfig;
	
	public boolean sendChequeEmail(RegisterObj r){
		GenericHTMLPreparator emailPrep = chequeEmailPrep(r);
		Map<String, Object> emailInfoMap = new HashMap<String, Object>();
		emailInfoMap.put("r", r);
		emailInfoMap.put("number", new NumberToolSg());
		emailPrep.setModel(emailInfoMap);
		mailSender.send(emailPrep);
		return true;
	}
	
	public boolean sendOtherCompanyEmail(RegisterObj r){
		GenericHTMLPreparator emailPrep = otherCompanyEmailPrep(r);
		Map<String, Object> emailInfoMap = new HashMap<String, Object>();
		emailInfoMap.put("r", r);
		emailInfoMap.put("number", new NumberToolSg());
		emailPrep.setModel(emailInfoMap);
		mailSender.send(emailPrep);
		return true;
	}
	
	public boolean sendParticipantListEmail(List<RegisterObj> registerList, RegisterFilter rf) {
		log.debug("sendParticipantListEmail() entry");
		if(registerList==null || rf==null)
			return false;
		for(RegisterObj r: registerList) {
			if(r.getParticipantList()==null
					||!r.getId().equals(rf.getId()))
				continue;
			for(ParticipantObj p: r.getParticipantList())
					sendParticipantEmail(p, r);
			return true;
		}
		return false;
	}
	
	public boolean sendParticipantListEmail(List<RegisterObj> registerList, ParticipantFilter pf) {
		log.debug("sendParticipantListEmail() entry");
		if(registerList==null || pf==null)
			return false;
		
		int i = 0;
		for(RegisterObj r: registerList) {
			if(r.getParticipantList()==null)
				continue;
			for(ParticipantObj p: r.getParticipantList()) 
				if(pf.getSelectedContentIds()[i++])
					sendParticipantEmail(p, r);
		}
		return true;
	}
	
	public boolean sendParticipantEmail(RegisterObj r){
		log.debug("sendParticipantEmail() entry");
		if(r.getParticipantSize()<=0)
			return false;
		for(ParticipantObj p: r.getParticipantList()) {
			sendParticipantEmail(p, r);
		}
		return true;
	}
	
	public boolean sendParticipantEmail(ParticipantObj p, RegisterObj r){
		GenericHTMLPreparator emailPrep = participantEmailPrep(p, r);
		Map<String, Object> emailInfoMap = new HashMap<String, Object>();
		emailInfoMap.put("p", p);
		emailInfoMap.put("number", new NumberToolSg());
		emailPrep.setModel(emailInfoMap);
		mailSender.send(emailPrep);
		return true;
	}
	
	protected GenericHTMLPreparator chequeEmailPrep(RegisterObj r) {
		GenericHTMLPreparator emailPrep = new GenericHTMLPreparator();
		emailPrep.setVelocityEngine(velocityEngine);
		emailPrep.setFrom(confirmationEmailConfig.getFrom());
		emailPrep.setTo(r.getEmail());
//		emailPrep.setTo(TEST_EMAIL);
//		emailPrep.setCc(CHERYL_EMAIL);
		emailPrep.setBcc(confirmationEmailConfig.getBcc());
		emailPrep.setSubject(chequeEmailConfig.getSubject());
		emailPrep.setSenderName(chequeEmailConfig.getSenderName());
		emailPrep.setTemplateLocation(chequeEmailConfig.getTemplateLocation());
		return emailPrep;
	}
	
	protected GenericHTMLPreparator otherCompanyEmailPrep(RegisterObj r) {
		GenericHTMLPreparator emailPrep = new GenericHTMLPreparator();
		emailPrep.setVelocityEngine(velocityEngine);
		emailPrep.setFrom(confirmationEmailConfig.getFrom());
		emailPrep.setTo(r.getEmail());
//		emailPrep.setTo(TEST_EMAIL);
//		emailPrep.setCc(CHERYL_EMAIL);
		emailPrep.setBcc(confirmationEmailConfig.getBcc());
		emailPrep.setSubject(othercompanyEmailConfig.getSubject());
		emailPrep.setSenderName(othercompanyEmailConfig.getSenderName());
		emailPrep.setTemplateLocation(othercompanyEmailConfig.getTemplateLocation());
		return emailPrep;
	}
	
	protected GenericHTMLPreparator participantEmailPrep(ParticipantObj p, RegisterObj r) {
		GenericHTMLPreparator emailPrep = new GenericHTMLPreparator();
		emailPrep.setVelocityEngine(velocityEngine);
		emailPrep.setFrom(confirmationEmailConfig.getFrom());
		emailPrep.setTo(p.getEmail());
		emailPrep.setCc(r.getEmail());
//		emailPrep.setTo(TEST_EMAIL);
		emailPrep.setBcc(confirmationEmailConfig.getBcc());
		emailPrep.setSubject(participantEmailConfig.getSubject());
		emailPrep.setSenderName(participantEmailConfig.getSenderName());
		emailPrep.setTemplateLocation(participantEmailConfig.getTemplateLocation());
		return emailPrep;
	}
	
	public boolean sendEmail(RegisterObj registerObj){
		log.debug("sendEmail() entry");
		
		return sendParticipantEmail(registerObj);
	}
	
	protected GenericHTMLPreparator getEmailPrep(RegisterObj r) {
		GenericHTMLPreparator emailPrep = new GenericHTMLPreparator();
		emailPrep.setVelocityEngine(velocityEngine);
		emailPrep.setFrom(confirmationEmailConfig.getFrom());
		emailPrep.setTo(r.getEmail());
//		emailPrep.setTo(TEST_EMAIL);
		emailPrep.setBcc(confirmationEmailConfig.getBcc());
		emailPrep.setSubject(participantEmailConfig.getSubject());
		emailPrep.setSenderName(participantEmailConfig.getSenderName());
		emailPrep.setTemplateLocation(participantEmailConfig.getTemplateLocation());
		return emailPrep;
	}
	
	//for sending email manually
	
	protected static final String TEST_EMAIL = "sherlyn@roxwell.com.sg";
	protected static final String CHERYL_EMAIL = "cheryl@roxwell.com.sg";
	private static EmailDao emailDao;
	private static String configLocation = "config/servlet-context.xml";
	public static void main(String[] args) {
		System.out.println("start");
		try {
			if(args!=null && args.length>=1)
				configLocation = args[0];
			initProcess(configLocation);
//			RegisterObj r = registrationDao.getRegisterObj(-2);
			
			emailDao.sendParticipantEmail(getP(), getR());
//			emailDao.sendOtherCompanyEmail(getR());
//			emailDao.sendChequeEmail(getR());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("EMailDao", e);
		}
		System.out.println("end");
	}
	
	protected static RegisterObj getR() {
		RegisterObj r = new RegisterObj();
		r.setFirstName("firsnm");
		r.setLastName("lastnm");
		r.setId(10048L);
		r.setEmail(TEST_EMAIL);
		return r;
	}
	
	protected static ParticipantObj getP() {
		ParticipantObj p = new ParticipantObj();
		p.setFirstName("firsnm");
		p.setLastName("lastnm");
		p.setId(10048L);
		p.setEmail(TEST_EMAIL);
		p.setMain(true);
		p.setPost1(true);
		p.setPost2(true);
		p.setBreakout1(1);
		p.setBreakout2(2);
		p.setPackageId(2);
		return p;
	}
	
	@SuppressWarnings("resource")
	protected static void initProcess(String configLocation) {
		ApplicationContext ctx = new ClassPathXmlApplicationContext(configLocation);
		emailDao = ctx.getBean(EmailDao.class);
	}
}
